<%@taglib prefix="itrust" uri="/WEB-INF/tags.tld"%>
<%@page errorPage="/auth/exceptionHandler.jsp"%>

<%@page import="java.util.List"%>
<%@page import="edu.ncsu.csc.itrust.beans.LabProcedureBean"%>
<%@page import="edu.ncsu.csc.itrust.action.EditPatientAction"%>
<%@page import="edu.ncsu.csc.itrust.action.LabProcHCPAction"%>
<%@page import="edu.ncsu.csc.itrust.beans.PatientBean"%>
<%@page import="edu.ncsu.csc.itrust.dao.mysql.PatientDAO"%>

<%@include file="/global.jsp"%>

<%
pageTitle = "iTrust - View Laboratory Procedures";
%>

<%@include file="/header.jsp"%>

<%
/* Require a Patient ID first */
String pidString = (String)session.getAttribute("pid");
if (pidString == null || 1 > pidString.length()) {
	response.sendRedirect("/iTrust/auth/getPatientID.jsp?forward=hcp/LabProcHCP.jsp");
   	return;
}
loggingAction.logEvent(TransactionType.LAB_RESULTS_VIEW, loggedInMID.longValue(), Long.parseLong(pidString), "Viewed laboratory procedures");
LabProcHCPAction action2 = new LabProcHCPAction(prodDAO, loggedInMID.longValue());
if(request.getParameter("priv")!=null && request.getParameter("priv").equals("yes")){
	action2.changePrivacy(Long.parseLong(request.getParameter("ID")));
	loggingAction.logEvent(TransactionType.LAB_RESULTS_VIEW, loggedInMID.longValue(), Long.parseLong(pidString), "Viewed laboratory procedures");
}

/* If the patient id doesn't check out, then kick 'em out to the exception handler */
EditPatientAction action = new EditPatientAction(prodDAO,loggedInMID.longValue(),pidString);
long pid = action.getPid();

List<LabProcedureBean> proc = action2.viewProcedures(pid);
%>

<br />
<table class="fTable">
	<tr>
		<th colspan="12">Lab Procedures</th>
	</tr>

	<tr class="subHeader">
		<td>Patient</td>
		<td>Lab Code</td>
		<td>Rights</td>
		<td>Status</td>
		<td>Lab Tech Name</td>
		<td>Commentary</td>
		<td>Results</td>
		<td>OfficeVisitID</td>
		<td>Updated Date</td>
		<td>Edit Office Visit</td>
		<td>Change Privacy</td>
		<td>Action</td>
	</tr>
	<%
	if(proc.size() > 0) {
		for(LabProcedureBean bean : proc){ 
			PatientBean patient = new PatientDAO(prodDAO).getPatient(bean.getPid());
%>
	<% String ltname = "Unassigned"; %>
	<% if(bean.getLtID() != 0){ %>
	<% ltname = prodDAO.getPersonnelDAO().getName(bean.getLtID()); %>
	<% } %>
	<tr>
		<td><%= StringEscapeUtils.escapeHtml("" + (patient.getFullName())) %></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (bean.getLoinc())) %></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (bean.getRights())) %></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (bean.getStatus())) %></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (ltname)) %></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (bean.getCommentary())) %></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (bean.getResults())) %></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (bean.getOvID())) %></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (bean.getTimestamp())) %></td>
		<td>
		<%if(action2.checkAccess(bean.getProcedureID())){%> <a
			href="/iTrust/auth/hcp-uap/editOfficeVisit.jsp?ovID=<%= StringEscapeUtils.escapeHtml("" + (bean.getOvID())) %>">Edit
		Office Visit</a><br />
		<%} %>
		</td>
		<td>
		<%if(action2.checkAccess(bean.getProcedureID())){%> <a
			href="/iTrust/auth/hcp/LabProcHCP.jsp?ID=<%= StringEscapeUtils.escapeHtml("" + (bean.getProcedureID())) %>&priv=yes">Allow/Disallow
		Viewing</a><br />
		<%} %>
		</td>
		<td><a
			href="/iTrust/auth/hcp/UpdateLabProc.jsp?ID=<%= StringEscapeUtils.escapeHtml("" + (bean.getProcedureID())) %>">Update</a><br />
		</td>

	</tr>
	<%
		}
	}
	else {
%>
	<tr colspan=10>
		<td align=center>No Data</td>
	</tr>
	<%		
	}
%>
</table>
<br />
<br />

<%@include file="/footer.jsp"%>
